var BuyerScroll = function(options) {
	this.SetOptions(options);
	this.lButton = this.options.lButton;
	this.rButton = this.options.rButton;
	this.oList = this.options.oList;
	this.showSum = this.options.showSum;
	this.iList = $("#" + this.options.oList + " > " + this.options.iList);
	this.initSrc = $("#" + this.options.oList + " > " + this.options.iList + " " + this.options.initSrc);
	this.ImgWidth = this.options.ImgWidth;
	this.ImgHeight = this.options.ImgHeight;
	this.iListSum = this.iList.length;
	this.iListWidth = this.iList.outerWidth(true);
	this.moveWidth = this.iListWidth * this.showSum;
	this.dividers = Math.ceil(this.iListSum / this.showSum);
	this.moveMaxOffset = (this.dividers - 1) * this.moveWidth;
	this.Init()
};
BuyerScroll.prototype = {
	SetOptions: function(options) {
		this.options = {
			lButton: "proint_lscroll",
			rButton: "proint_rscroll",
			oList: "proint_scroll",
			iList: ".box_col",
			initSrc: ".pro_link",
			showSum: 4,
			ImgWidth: 100,
			ImgHeight: 100
		};
		$.extend(this.options, options || {})
	},
	Init: function() {
		var pItemsLenght = (this.showSum * this.dividers) - this.iListSum,
		_iList = this.options.iList.split(".");
		if (pItemsLenght != 0) {
			for (var j = 0; j < pItemsLenght; j++) {
				$("#" + this.oList).append("<div class=" + _iList[1] + "></div>")
			}
			this.iList = $("#" + this.options.oList + " > " + this.options.iList);
			this.iListSum = this.iList.length
		}
		this.CreateImage();
		for (var i = 0; i < this.showSum; i++) {
			this.LoadImage(i)
		}
		this.LeftScroll();
		this.RightScroll()
	},
	ReturnLeft: function() {
		return isNaN(parseInt($("#" + this.oList).css("left"))) ? 0 : parseInt($("#" + this.oList).css("left"))
	},
	LeftScroll: function() {
		if (this.dividers == 1) return;
		var _this = this,
		currentOffset;
		$("#" + this.lButton).mouseover(function() {
			$(this).attr("className", "hover_lsku")
		});
		$("#" + this.lButton).mouseout(function() {
			$(this).attr("className", "defult_lsku")
		});
		$("#" + this.lButton).mousedown(function() {
			$(this).attr("className", "click_lsku")
		});
		$("#" + this.lButton).mouseup(function() {
			$(this).attr("className", "hover_lsku")
		});
		$("#" + this.lButton).click(function() {
			currentOffset = _this.ReturnLeft();
			if (currentOffset == 0) {
				for (var i = 1; i <= _this.showSum; i++) {
					$(_this.iList[_this.iListSum - i]).prependTo($("#" + _this.oList))
				}
				$("#" + _this.oList).css({
					left: -_this.moveWidth
				});
				$("#" + _this.oList + ":not(:animated)").animate({
					left: "+=" + _this.moveWidth
				},
				{
					duration: "slow",
					complete: function() {
						for (var j = _this.showSum + 1; j <= _this.iListSum; j++) {
							$(_this.iList[_this.iListSum - j]).prependTo($("#" + _this.oList))
						}
						$("#" + _this.oList).css({
							left: -_this.moveWidth * (_this.dividers - 1)
						});
						_this.CurrentLoad()
					}
				})
			} else {
				$("#" + _this.oList + ":not(:animated)").animate({
					left: "+=" + _this.moveWidth
				},
				{
					duration: "slow",
					complete: function() {
						_this.CurrentLoad()
					}
				})
			}
		})
	},
	RightScroll: function() {
		if (this.dividers == 1) return;
		var _this = this,
		currentOffset;
		$("#" + this.rButton).mouseover(function() {
			$(this).attr("className", "hover_rsku")
		});
		$("#" + this.rButton).mouseout(function() {
			$(this).attr("className", "defult_rsku")
		});
		$("#" + this.rButton).mousedown(function() {
			$(this).attr("className", "click_rsku")
		});
		$("#" + this.rButton).mouseup(function() {
			$(this).attr("className", "hover_rsku")
		});
		$("#" + this.rButton).click(function() {
			currentOffset = _this.ReturnLeft();
			if (Math.abs(currentOffset) >= _this.moveMaxOffset) {
				for (var i = 0; i < _this.showSum; i++) {
					$(_this.iList[i]).appendTo($("#" + _this.oList))
				}
				$("#" + _this.oList).css({
					left: -_this.moveWidth * (_this.dividers - 2)
				});
				$("#" + _this.oList + ":not(:animated)").animate({
					left: "-=" + _this.moveWidth
				},
				{
					duration: "slow",
					complete: function() {
						for (var j = _this.showSum; j < _this.iListSum; j++) {
							$(_this.iList[j]).appendTo($("#" + _this.oList))
						}
						$("#" + _this.oList).css({
							left: 0
						});
						_this.CurrentLoad()
					}
				})
			} else {
				$("#" + _this.oList + ":not(:animated)").animate({
					left: "-=" + _this.moveWidth
				},
				{
					duration: "slow",
					complete: function() {
						_this.CurrentLoad()
					}
				})
			}
		})
	},
	CreateImage: function() {
		this.image = [],
		this.image_src = [];
		for (var i = 0; i < this.initSrc.length; i++) {
			this.image_src.push($(this.initSrc[i]).attr("init"));
			this.image.push(new Image())
		}
	},
	LoadImage: function(i) {
		if (!$(this.initSrc[i]).attr("init")) {
			return
		}
		var _this = this,
		size = [];
		(function() {
			var index = i;
			$(_this.image[index]).load(function() {
				if (_this.image[index].width == 0 || _this.image[index].height == 0) return;
				size.push(_this.AutoScaling(_this.image[index], index));
				$(_this.initSrc[index]).append("<img src=" + $(_this.initSrc[i]).attr("init") + " width=" + size[0][0] + " height=" + size[0][1] + " />");
				$(_this.initSrc[index]).removeAttr("init");
				_this.image[index] = new Image();
				size = []
			});
			_this.image[index].src = _this.image_src[index]
		})()
	},
	AutoScaling: function(source, index) {
		var iWidth, iHeight;
		if (source.width > 0 && source.height > 0) {
			if (source.width / source.height >= this.ImgWidth / this.ImgHeight) {
				if (source.width > this.ImgWidth) {
					iWidth = this.ImgWidth;
					iHeight = (source.height * this.ImgWidth) / source.width
				} else {
					iWidth = source.width;
					iHeight = source.height
				}
			} else {
				if (source.height > this.ImgHeight) {
					iHeight = this.ImgHeight;
					iWidth = (source.width * this.ImgHeight) / source.height
				} else {
					iWidth = source.width;
					iHeight = source.height
				}
			}
		}
		source = new Image();
		return [parseInt(iWidth), parseInt(iHeight)]
	},
	CurrentLoad: function() {
		var k = Math.abs(parseInt(this.ReturnLeft() / this.iListWidth));
		for (var j = k; j <= k + (this.showSum - 1); j++) {
			this.LoadImage(j)
		}
	}
};